package com.my.core.util;


import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 字符串处理工具类
 * @author zengjintao
 * @version 1.0
 * @create_at 2017年6月18日 下午10:02:37
 */
public class ObjectUtils {

	/**
	 * 将字符串转换成数组
	 * @param str
	 * @return
	 */
	public static String[] spilt(String str) {
		if (str == null)
			throw new NullPointerException("str can not be null");
		return str.split(",");
	}

	public static List<String> spiltToList(String str) {
		if (str == null)
			throw new NullPointerException("str can not be null");
		String[] array = str.split(",");
		return Arrays.asList(array);
	}

	public static String[] spilt(String str, String separator) {
		if (str == null)
			throw new NullPointerException("str can not be null");
		return str.split(separator);
	}

	/**
	 * 将首字母变成小写
	 * @param keyName
	 * @return
	 */
	public static String toLowerCaseFirst(String keyName){
		return keyName.substring(0, 1).toLowerCase();
	}

	/**
	 * 将首字母变成大写
	 * @param keyName
	 * @return
	 */
	public static String totoUpperCaseFirst(String keyName) {
		return keyName.substring(0, 1).toUpperCase() + keyName.substring(1, keyName.length());
	}

	/**
	 * 生成年月日字符串
	 * @return
	 */
	public static String generateFileByTime() {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date()).replaceAll("-", "/");
	}

	/**
	 * 生成时分秒字符串
	 * @return
	 */
	public static String generateFileBySecond() {
		DateFormat format = new SimpleDateFormat("hh-mm-ss");
		return format.format(new Date()).replaceAll("-", "");
	}

	public static String generateUuId() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static String charSort(String value) {
		char[] c = value.toCharArray(); //将字符串转换成char数组
		Arrays.sort(c);//对数组进行排序
		return new String(c); //返回数组。注
	}


	/**
	 * @Description 对象转map，作用于public描述的字段
	 * @Author SR
	 * @Param
	 */
	public static HashMap<String, Object> object2Map(Object object) {
		HashMap<String, Object> map = new HashMap<>();
		try {
			Class<?> clazz = object.getClass();
			for (Field field : clazz.getFields()) {
				field.setAccessible(true);
				String fieldName = field.getName();
				Object value = field.get(object);
				map.put(fieldName, value);
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			System.out.println("对象转map失败：object2Map");
			map = null;
		}
		return map;
	}

	public static Map<String, List> object2MapEmptyList(Object object) {
		Map<String, List> map = new HashMap<>();
		try {
			Class<?> clazz = object.getClass();
			for (Field field : clazz.getFields()) {
				field.setAccessible(true);
				String fieldName = field.getName();
				map.put(fieldName, new ArrayList<>());
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("对象转map失败：object2MapEmptyList");
			map = null;
		}
		return map;
	}

	public static Map<String, List> object2MapList(Object object) {
		Map<String, List> map = new HashMap<>();
		try {
			Class<?> clazz = object.getClass();
			for (Field field : clazz.getFields()) {
				field.setAccessible(true);
				String fieldName = field.getName();
				List<String> value = new ArrayList<>();
				value.add(field.get(object).toString());
				map.put(fieldName, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("对象转map失败：object2MapEmptyList");
			map = null;
		}
		return map;
	}

	public static HashMap objectList2MapList(List list) {
		HashMap<String, List> map = new HashMap<>();
		try {
			if (list == null || list.size() == 0) {
				throw new Exception("list不能为空");
			}
			for (Object object : list) {
				Class<?> clazz = object.getClass();
				for (Field field : clazz.getFields()) {
					field.setAccessible(true);
					List value;
					String fieldName = field.getName();
					if (map.keySet().contains(fieldName)) {
						value = map.get(fieldName);
					} else {
						value = new ArrayList<>();
					}
					Object o = field.get(object);
					value.add(o!=null?o.toString():"");
					map.put(fieldName, value);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("对象转map失败：objectList2MapList");
			map = null;
		}
		return map;
	}
}
